package com.comdo.newup_loan.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.comdo.newup_loan.R;
import com.comdo.newup_loan.model.RePayDetailBean;
import com.comdo.newup_loan.module.repayment_action.PayOffActivity;
import com.comdo.newup_loan.module.repayment_action.RepayActivity;
import com.comdo.newup_loan.module.repayment_action.XudaiActivity;
import com.comdo.newup_loan.module.repayment_action.XudaiApplyActivity;
import com.comdo.newup_loan.util.DisplayUtil;
import com.comdo.newup_loan.util.StringUtils;

import java.util.List;

/**
 * Created by liyuan on 2017/9/27.
 */

public class RepayDetailAdapter extends RecyclerView.Adapter<RepayDetailAdapter.ViewHolder> implements View.OnClickListener {
    public List<RePayDetailBean.RePayPlan> datas = null;
    private OnItemClickListener mOnItemClickListener = null;
    private Context context;
    private final int screenWidth;

    public RepayDetailAdapter(List<RePayDetailBean.RePayPlan> datas, Context context) {
        this.datas = datas;
        this.context = context;
        screenWidth = DisplayUtil.getScreenWidth(context);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        viewHolder.itemView.setTag(position);
        viewHolder.mIndexText.setText(datas.get(position).getIndexs()+"");
        viewHolder.mAmountText.setText(StringUtils.getMoneyString(datas.get(position).getSupportBackAmount()));
        viewHolder.mTimeText.setText(datas.get(position).getRepayTime());
        viewHolder.mActualTimeText.setText(datas.get(position).getActualRepayTime());
        viewHolder.mRemarkText.setText(datas.get(position).getRemarks());
        viewHolder.mRePayBtn.setVisibility(datas.get(position).getShowRepayButton()==1?View.VISIBLE:View.GONE);
        viewHolder.mXdApplyBtn.setVisibility(datas.get(position).getShowXudaiApplyButton()==1?View.VISIBLE:View.GONE);
        viewHolder.mXdBtn.setVisibility(datas.get(position).getShowXudaiButton()==1?View.VISIBLE:View.GONE);
        viewHolder.mEarlyRePayBtn.setVisibility(datas.get(position).getEarlyRepayButton()==1?View.VISIBLE:View.GONE);

        ViewGroup.LayoutParams params1=viewHolder.mRePayBtn.getLayoutParams();
        params1.width=screenWidth/4-20;
        viewHolder.mRePayBtn.setLayoutParams(params1);
        ViewGroup.LayoutParams params2=viewHolder.mXdApplyBtn.getLayoutParams();
        params2.width=screenWidth/4-20;
        viewHolder.mXdApplyBtn.setLayoutParams(params2);

        ViewGroup.LayoutParams params3=viewHolder.mXdBtn.getLayoutParams();
        params3.width=screenWidth/4-20;
        viewHolder.mXdBtn.setLayoutParams(params3);

        ViewGroup.LayoutParams params4=viewHolder.mEarlyRePayBtn.getLayoutParams();
        params4.width=screenWidth/4-20;
        viewHolder.mEarlyRePayBtn.setLayoutParams(params4);

        viewHolder.mRePayBtn.setId(datas.get(position).getId());
        viewHolder.mXdApplyBtn.setId(datas.get(position).getId());
        viewHolder.mXdBtn.setId(datas.get(position).getId());
        viewHolder.mEarlyRePayBtn.setId(datas.get(position).getId());
        viewHolder.mRePayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, RepayActivity.class);
                intent.putExtra("repayPlanId",v.getId());
                context.startActivity(intent);
            }
        });
        viewHolder.mXdApplyBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, XudaiApplyActivity.class);
                intent.putExtra("repayPlanId",v.getId());
                context.startActivity(intent);
            }
        });
        viewHolder.mXdBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(context, XudaiActivity.class);
                intent.putExtra("repayPlanId",v.getId());
                context.startActivity(intent);
            }
        });
        viewHolder.mEarlyRePayBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Intent intent=new Intent(context, PayOffActivity.class);;
                intent.putExtra("repayPlanId",v.getId());
                context.startActivity(intent);
            }
        });
        if(StringUtils.isNull(datas.get(position).getRemarks())){
            viewHolder.mRemarkText.setVisibility(View.GONE);
        }else{
            viewHolder.mRemarkText.setVisibility(View.VISIBLE);
        }
        if(datas.get(position).getXudaiState()==2){
            viewHolder.mXdApplyBtn.setText("再次申请");
        }else {
            viewHolder.mXdApplyBtn.setText("续贷申请");
        }
        viewHolder.mRemarkText.setId(position);
        viewHolder.mRemarkText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!StringUtils.isNull(datas.get(v.getId()).getRemarks()))
                Toast.makeText(context,datas.get(v.getId()).getRemarks(),Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_repay_planlist, parent, false);
        ViewHolder vh = new ViewHolder(view);
        view.setOnClickListener(this);
        return vh;
    }

    @Override
    public int getItemCount() {
        return datas.size();
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public TextView mIndexText;
        public TextView mAmountText;
        public TextView mTimeText;
        public TextView mActualTimeText;
        public TextView mRemarkText;
        public Button mRePayBtn;
        public Button mXdApplyBtn;
        public Button mXdBtn;
        public Button mEarlyRePayBtn;
        public ViewHolder(View view) {
            super(view);
            mIndexText = (TextView) view.findViewById(R.id.tv_indexs);
            mAmountText = (TextView) view.findViewById(R.id.tv_amount);
            mTimeText = (TextView) view.findViewById(R.id.tv_date);
            mActualTimeText = (TextView) view.findViewById(R.id.tv_actual_date);
            mRemarkText = (TextView) view.findViewById(R.id.tv_remarks);
            mRePayBtn=(Button)view.findViewById(R.id.btn_repay);
            mXdApplyBtn=(Button)view.findViewById(R.id.btn_xd_apply);
            mXdBtn=(Button)view.findViewById(R.id.btn_xd);
            mEarlyRePayBtn=(Button)view.findViewById(R.id.btn_early_repay);

        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }
}
